package com.gxc.datacore.metadata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.datacore.metadata.domain.Column;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class ColumnService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public Column retrieve(String id) {
		return beanService.get(sqlDao, Column.class, id);
	}

	public int create(Column column) {
		return beanService.create(sqlDao, column);
	}

	public int update(Column column) {
		return beanService.update(sqlDao, column);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, Column.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, Column.class, idArray);
	}

	@SuppressWarnings("rawtypes")
	public List list(Map parameter) {
		return sqlDao.selectList("datacore.column.list", parameter);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map parameter, PageInfo pageInfo) {
		return sqlDao.selectPageList("datacore.column.list", parameter,
				pageInfo);
	}

	/**
	 * 删除table_id的所有列
	 * 
	 * @param table_id
	 * @return
	 */
	public int deleteByTableId(String table_id) {
		return sqlDao.delete("datacore.column.deleteByTableId", table_id);
	}

	/**
	 * 根据table_id、column_name获取column对象
	 * 
	 * @param table_id
	 * @param column_name
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Column retrieveByName(String table_id, String column_name) {
		Map params = new HashMap();
		params.put("table_id", table_id);
		params.put("name", column_name);
		return sqlDao.selectOne("datacore.column.retrieveByName", params);
	}

	/**
	 * 查询角色有权访问的列
	 * 
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List listByRoles(Map params) {
		return sqlDao.selectList("datacore.column.listByRoles", params);
	}

	/**
	 * 查询角色有权访问的列
	 * 
	 * @param parameter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public PageList listPageByRoles(Map parameter, PageInfo pageInfo) {
		return sqlDao.selectPageList("datacore.column.listByRoles", parameter,
				pageInfo);
	}
}
